<?php
/**
 * Description of afreport
 *
 * @author Bojan Mitrovic
 */
require_once('../config.php');
require_once('lib.php');
class afreport {
    var $forumid;
    var $studentsSQL;
    var $courseid;
    public function  __construct($forumid, $courseid){
        global $CFG;
        $this->forumid = $forumid;
        $this->courseid = $courseid;
        $this->studentsSQL = "SELECT DISTINCT userid, firstname, lastname, idnumber FROM {$CFG->prefix}aformat_forum_posts, {$CFG->prefix}aformat_forum_themes, {$CFG->prefix}user WHERE {$CFG->prefix}user.id=userid AND {$CFG->prefix}aformat_forum_themes.id=themeid AND forumid=$forumid ORDER BY firstname";
    }
    public function reportScore($fId,$uId){
        global $CFG;
        $score = 0;
        $scoreSQL = "SELECT themeid, (MIN(posted)-datestart) AS firstPostAfter, datestart, penalpointsafterdays, maxpoints FROM {$CFG->prefix}aformat_forum_posts, {$CFG->prefix}aformat_forum_themes WHERE themeid={$CFG->prefix}aformat_forum_themes.id  AND forumid=$fId AND userid=$uId GROUP BY themeid";
        if ($rsScore=get_recordset_sql($scoreSQL)) {
            while($recordScore = rs_fetch_next_record($rsScore)){
                $score += $recordScore->maxpoints;
                $interval = (int)($recordScore->firstPostAfter)/086400;
                $penals = explode(";",$recordScore->penalpointsafterdays);
                foreach($penals as $penal){
                    $dp = explode(":",$penal);
                    if($interval>$dp[0]){
                        $score-= $dp[1];
                        break;
                    }
                }
            }
            $rsScore->close();
        }
        return $score;
    }
    public function reportTable(){
        global $CFG;
        $rv = '';
        $rv.= '<table width="100%" style="border:solid 1px black">';
        if ($rs=get_recordset_sql($this->studentsSQL)) {
            $rowNumber = 0;
            $rv.= '<tr><td></td><td>'.get_string('firstname').'</td><td>'.get_string('lastname').'</td><td>'.get_string('idnumber').'</td><td align="right">'.get_string('grade').'</td>';
            $cgNames = get_recordset_sql("SELECT type FROM {$CFG->prefix}aformat_grade_type, {$CFG->prefix}aformat_forum_grade_types WHERE {$CFG->prefix}aformat_grade_type.id=gradetypeid AND forumid=$this->forumid ORDER BY gradetypeid ASC");
            if($cgNames){
                while ($cgnr = rs_fetch_next_record($cgNames)) {
                    $rv.= "<td align=\"right\">$cgnr->type</td>";
                }
                $cgNames->close();
            }
            $rv.= "<td align=\"right\">total</td>";
            $rv.= '</tr>';
            while ($record = rs_fetch_next_record($rs)) {
                $rowNumber+= 1;
                if(isstudent($this->courseid, $record->userid)&&!isteacher($this->courseid, $record->userid)){
                    //Link to profile
                    //$rv.= '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$record->userid.'">';
                    //Link to collecion
                    $rv.= '<tr onclick="window.location=\''.$CFG->wwwroot.'/course/view.php?id='.$this->courseid.'&aformatcollection='.$record->userid.'\';" style="border:solid 1px black; margin-left:50px">';
                    $rv.= "<td align=\"right\" style=\"padding-right:5px\">$rowNumber.</td>";
                    $rv.= "<td align=\"left\" style=\"padding-right:15px\">$record->firstname</td>";
                    $rv.= "<td align=\"left\" style=\"padding-right:15px\">$record->lastname</td>";
                    $rv.= "<td align=\"left\" style=\"padding-right:5px\">$record->idnumber</td>";
                    $total = $this->reportScore($this->forumid, $record->userid);
                    $rv.= "<td align=\"right\" style=\"padding-right:5px\">{$total}</td>";
                    $customGrades = get_recordset_sql("SELECT grade FROM {$CFG->prefix}aformat_grades WHERE forumid={$this->forumid} AND userid=$record->userid ORDER BY gradetypeid ASC");
                    if ($customGrades) {
                        while ($rsg = rs_fetch_next_record($customGrades)) {
                            $rv.= "<td align=\"right\" style=\"padding-right:5px\">$rsg->grade</td>";
                            $total+=$rsg->grade;
                        }
                        $customGrades->close();
                    }
                    $rv.= "<td align=\"right\" style=\"padding-right:5px\">{$total}</td>";
                    $rv.= '</tr>';
                }
            }
            $rs->close();
        }
        $rv.= '</table>';
        return $rv;
    }
}
?>